package com.ebt.m.wiki;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.data.bean.Brand;
import com.ebt.m.data.bean.TempDividerBrand;
import com.ebt.m.proposal_v2.event.BrandFilterEvent;
import com.ebt.m.wiki.event.EFilter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wanglu on 2017/10/18.
 */

public class SectionProductFilterBrand extends IView {
    Brand brand;
    public boolean needClose = false;
    @BindView(R.id.brand_name)
    TextView brandName;
    @BindView(R.id.iv_cb)
    ImageView ivCb;
    @BindView(R.id.item_root)
    RelativeLayout itemRoot;
    @BindView(R.id.divider)
    View divider;
    @BindView(R.id.open_insurance)
    View openInsurance;
    private boolean isChecked;

    public SectionProductFilterBrand(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inflate(context, R.layout.product_selection_item_4_brand, this);
        ButterKnife.bind(this);

    }

    public SectionProductFilterBrand(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SectionProductFilterBrand(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... args) {
        brand = (Brand) args[0];
        brandName.setText(brand.getBrandShortName());
        try {
            TempDividerBrand dividerBrand = (TempDividerBrand) args[1];
            Brand selectedBrand = dividerBrand.getSelectedBrand();
            if (selectedBrand != null) {
                isChecked = selectedBrand.getBrandId() == brand.getBrandId();
                ivCb.setVisibility(isChecked ? View.VISIBLE : View.GONE);
            }
            divider.setVisibility(dividerBrand.isShowDivider() ? View.VISIBLE : View.GONE);
            openInsurance.setVisibility(brand.getIsOpenInsurance() == 1 ? View.VISIBLE : View.GONE);
        } catch (Exception e) {
            ivCb.setVisibility(View.GONE);
        }
    }

    @OnClick(R.id.item_root)
    public void onViewClicked() {
        if (!isChecked) {
            isChecked = true;
            ivCb.setVisibility(isChecked ? View.VISIBLE : View.GONE);
            if (needClose) {
                EventBus.getDefault().post(new EFilter(brand));
                ((AppCompatActivity) getContext()).finish();
            } else {
                EventBus.getDefault().post(new BrandFilterEvent(brand));
            }
        }
    }
}
